package com.lucky.common.base;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lucky.common.pojo.po.AreaPO;
import com.lucky.common.service.AreaService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.ui.ModelMap;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import javax.servlet.http.HttpServletRequest;

public class BaseController {
	
	public Logger log = LoggerFactory.getLogger(BaseController.class);
	
	@Resource
	public AreaService areaService;
	
	@Value("${server.servlet.context-path}")
	private String contextPath;

	/**
	 * 封装page信息，与前台约定参数，page为页码，rows为显示的条数 页面信息包含pageNo，pageSize
	 * 
	 * @param request
	 * @return
	 */
	public PagerVO getPager(HttpServletRequest request) {
		PagerVO page = new PagerVO();
		String page_size = request.getParameter("pageSize");
		String pageNum = request.getParameter("pageNum");
		if (null == pageNum || "".equals(pageNum)) {
			page.setPageNum(1);
		} else {
			page.setPageNum(Integer.parseInt(pageNum));
		}
		if (null == page_size || "".equals(page_size)) {
			page.setPageSize(10);
		} else {
			page.setPageSize(Integer.parseInt(page_size));
		}
		return page;
	}
	
	/**
	 * 封装page信息，与前台约定参数，page为页码，rows为显示的条数 页面信息包含pageNo，pageSize
	 * 
	 * @return
	 */
	public PagerVO getPager(PageDTO page) {
		PagerVO vo = new PagerVO();
		int page_size = page.getPageSize();
		int pageNum = page.getPageNum();
		if (0 == pageNum ) {
			vo.setPageNum(1);
		} else {
			vo.setPageNum(pageNum);
		}
		if (0 == page_size ) {
			vo.setPageSize(10);
		} else {
			vo.setPageSize(page_size);
		}
		return vo;
	}
	
	public Integer getStartIndex(PagerVO page) {
		int num = page.getPageNum();
		int size = page.getPageSize();
		int result = (num-1)*size;
		return result;
	}
	
	public String getRandom() {
		return Math.round((Math.random()+1)*1000)+"";
	}
	
	public RedirectView getRedirectView(String url) {
		return new RedirectView(url, true, false);
	}
	
	public ModelAndView redirectView(String url) {
		RedirectView redirectView = new RedirectView(url, true, false);
		return new ModelAndView(redirectView);
	}
	
	
	public String getAreaMsg(String province,String city,String county){
		String address = "";
		String countyImg = "";
		String cityImg = "";
		String provinceImg = "";
		if(county!=null && !"".equals(county)){
			AreaPO qap = new AreaPO();
			qap.setAreacode(county);
			AreaPO acocounty = areaService.getOne(new QueryWrapper(qap));
			countyImg = acocounty.getAreaname();
		}
		if(city!=null && !"".equals(city)){
			AreaPO qap = new AreaPO();
			qap.setAreacode(city);
			AreaPO acity = areaService.getOne(new QueryWrapper(qap));
			cityImg = acity.getAreaname();
		}
		if(province!=null && !"".equals(province)){
			AreaPO qap = new AreaPO();
			qap.setAreacode(province);
			AreaPO aprovince = areaService.getOne(new QueryWrapper(qap));
			provinceImg = aprovince.getAreaname();
		}
		address = provinceImg+"-"+cityImg+"-"+countyImg;
		return address;
	}
	
	
	
//	public <E> PageInfo<E> getPageInfo(Page<E> page) {
//		return new PageInfo<E>(page);
//	}



	protected String errorAidNotMatch(ModelMap model){
		log.info("无权限访问");
		model.put("msg","无权限访问");
		return "base/error/aidNotMatch";
	};

	protected String alertContentReload(ModelMap model, String msg){
		model.put("msg",msg);
		return "base/bridge/alertContentReload";
	};
	protected String parentReload(ModelMap model){
		return "base/bridge/parentReload";
	}
	protected String topReload(ModelMap model){
		return "base/bridge/topReload";
	}
	protected String alertParentCloseFrameWindow(ModelMap model, String msg){
		model.put("msg",msg);
		return "base/bridge/alertParentCloseFrameWindow";
	};

	protected String alertParentReload(ModelMap model, String msg){
		model.put("msg",msg);
		return "base/bridge/alertParentReload";
	}
	protected String alert(ModelMap model, String msg){
		model.put("msg",msg);
		return "base/bridge/alert";
	}
	protected String alertLocation(ModelMap model, String msg,String url){
		model.put("msg",msg);
		model.put("url",url);
		return "base/bridge/alertLocation";
	}
	protected String alertBack(ModelMap model, String msg){
		model.put("msg",msg);
		return "base/bridge/alertBack";
	}
	protected String alertParentLocation(ModelMap model, String msg,String url){
		model.put("msg",msg);
		model.put("url",url);
		return "base/bridge/alertParentLocation";
	}

	protected String getContextPath(){
		return contextPath;
	}

}
